package servlet;

import com.alibaba.fastjson.JSON;
import entity.User;
import service.RedisService;
import service.SmsService;
import service.UserService;
import service.UserServiceImpl;
import service.impl.RedisServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/user_rigister")
public class User_rigisterServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        UserService userService = new UserServiceImpl();
        User Email = null;
        User userName = null;
        // 获取手机号码
        String phone  = request.getParameter("phone");
        // 获取验证码
        String verify  = request.getParameter("verify");
        // 获取邮箱
        String email = request.getParameter("email");
        // 获取username
        String username = request.getParameter("username");

        // 创建RedisService
        RedisService redisService = new RedisServiceImpl();
        String redisCode = redisService.get(phone);
        // 保存code到redis
        Map map = new HashMap();
        PrintWriter out = response.getWriter();
        int num = 0;
        try {
            Email = userService.emails(email);
            userName = userService.usernames(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (verify != null && verify.equals(redisCode)) {
            if (Email == null && userName == null) {
                Email = new User();
                Email.setUsername(request.getParameter("username"));
                Email.setEmail(request.getParameter("email"));
                Email.setPassword(request.getParameter("password"));
                Email.setName(request.getParameter("name"));
                Email.setPhone(request.getParameter("phone"));
                Email.setAddress(request.getParameter("address"));
                Email.setIsadmin(0);
                Email.setIsvalidate(0);
                try {
                    num = userService.register(Email);
                    if (num == 1) {
                        Email = userService.phones(phone);
                        request.getSession().setAttribute("loginUser", Email);
                        response.sendRedirect(request.getContextPath() + "/user_login.jsp");
                    } else {
                        out.print("<script>alert('注册失败!');history.back();</script>");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (userName != null){
                out.print("<script>alert('用户名已被使用!');history.back();</script>");
            }else if (Email != null){
                out.print("<script>alert('邮箱已被使用!');history.back();</script>");
            }
        }else {
//            request.getSession().setAttribute("error","验证码错误,请重新输入!");
//            response.sendRedirect("user_register.jsp");
            out.print("<script>alert('验证码错误,请重新输入!');history.back();</script>");
        }
        out.flush();
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
